// SupportHangerManager.cpp: implementation of the SupportHangerManager class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "SupportHangerManager.h"
#include "SupportHangerGenerator.h"
#include "ProjectManager.h"
#include "DBConnectionManager.h"
#include "PersistentObjectManager.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

SupportHangerManager::SupportHangerManager():
ReferenceManager<CString, UeObjectId, SupportHanger*>(203)
{
}

SupportHangerManager::~SupportHangerManager()
{

}

SupportHanger* SupportHangerManager::GetSupportHanger(const CString& strSHID,
				const CString& strVolumeID, const CString& strPrjID)
{
	CString strTempVolumeID = strVolumeID;
	CString strTempPrjID = strPrjID;

	if(strTempVolumeID.IsEmpty())
	{
		ASSERT(GetProjectManager().CurProject());
		ASSERT(GetProjectManager().CurProject()->GetVolumeManager().CurVolume());
		strTempVolumeID = ltos(GetProjectManager().CurProject()
			->GetVolumeManager().CurVolume()->VolumeID());
		ASSERT(!strTempVolumeID.IsEmpty());
	}

	CString strKey = strSHID;
	
	return this->GetManaged(strKey);
/*	if(pSH != NULL)
	{
		if(!pSH->IsReadAll())
		{
			ASSERT(project());
			ASSERT(project()->versionController());
			this->project()->versionController()->CurSupportHangerGenerator().ReadAllSHData(pSH);
		}
		return pSH;
	}*/
}

SupportHanger* SupportHangerManager::GetSupportHanger(const SupportHangerPoint& pt)
{
	ASSERT(FALSE);
	ASSERT(!pt.SupportHangerID().IsEmpty());
	ASSERT(!pt.VolumeID().IsEmpty());

	CString strKey = pt.SupportHangerID();
//	SupportHanger* pSH = this->GetManaged(strKey);

	return this->GetManaged(strKey);
/*	if(pSH != NULL)
	{
		if(!pSH->IsReadAll())
		{
			ASSERT(project());
			ASSERT(project()->versionController());
			this->project()->versionController()->CurSupportHangerGenerator().ReadAllSHData(pSH);
		}
		return pSH;
	}*/
//	return pSH;
}

SupportHanger* SupportHangerManager::NewSupportHanger(const CString& strSHID,
													  const CString& strVolumeID, const CString& strPrjID)
{
	ASSERT(FALSE);
	SupportHanger *pSH = NULL;
	pSH = new SupportHanger;
	ASSERT(pSH);

	pSH->GetSupportHangerPoint().VolumeID(strVolumeID);
	pSH->GetSupportHangerPoint().SupportHangerID(strSHID);
	return pSH;
}

void SupportHangerManager::AddSupportHanger(SupportHanger* pSH)
{
	ASSERT(pSH);
	ASSERT(!pSH->SupportHangerID().IsEmpty());
	ASSERT(this->project());

	pSH->project(this->project());
	pSH->GetSupportHangerPoint().Database(project()->ProjectSource());
	pSH->GetSupportHangerPoint().PrjID(project()->ProjectName());

	CString strKey = pSH->SupportHangerID();

	project()->GetPersistentObjectManager().Add(pSH);
	m_pCurHanger = pSH;
	this->Add(strKey, pSH->Id());
}

CString SupportHangerManager::SupportHangerIndex()
{
	static INT nIndex = 1;

	Project* pPrj = GetProjectManager().CurProject();
	ASSERT(pPrj);
	Volume* pVolume = pPrj->GetVolumeManager().CurVolume();
	ASSERT(pVolume);

	CString strSHID;
	while(true)
	{
		strSHID = pVolume->Name() + _T("_") + ltos(nIndex);
		if(!this->IsExist(strSHID))
			break;
		nIndex++;
	}
	return strSHID;
}
